import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/village_grid_component/bean/village_grid_response.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    VillageHousePickerState state, Dispatch dispatch, ViewService viewService) {
  return Column(
    children: <Widget>[
      Container(
        color: Colors.grey,
        child: Row(
          children: _createNaviViews(state, dispatch, viewService),
        ),
      ),
      Expanded(
        flex: 1,
        child: ListView.builder(
          itemCount: state.datas.length,
          itemBuilder: (ctx, position) {
            var data = state.datas[position];
            if (data is VillageBuildGridResult) {
              return ListTile(
                title: Text(data.buildingName),
                onTap: () {
                  dispatch(VillageHousePickerActionCreator.chooseBuildInfo(
                      build: data));
                },
              );
            } else if (data is VillageUnitGridResult) {
              return ListTile(
                title: Text(data.itemName),
                onTap: () {
                  dispatch(VillageHousePickerActionCreator.chooseUnitInfo(
                      build: data));
                },
              );
            } else if (data is VillageHouseGridResult) {
              return ListTile(
                title: Text(data.roomName),
                onTap: () {
                  dispatch(VillageHousePickerActionCreator.chooseHouseInfo(
                      build: data));
                },
              );
            } else {
              return ListTile(
                title: Text('what?'),
              );
            }
          },
        ),
      ),
    ],
  );
}

List<Widget> _createNaviViews(
    VillageHousePickerState state, Dispatch dispatch, ViewService viewService) {
  var list = List<Widget>();
  list.add(Expanded(
    child: FlatButton(
      child: Text(
        state.selectedBuildInfo == null
            ? '全部楼栋'
            : state.selectedBuildInfo.buildingName,
        style: TextStyle(
          color: state.mode == HousePickerMode.BUILD
              ? Colors.blueAccent
              : Colors.black,
        ),
      ),
      onPressed: () {
        dispatch(VillageHousePickerActionCreator.chooseBuildInfo());
      },
    ),
  ));
  list.add(Text('>'));

  list.add(Expanded(
    child: FlatButton(
      child: Text(
        state.selectedUnitInfo != null ? state.selectedUnitInfo.itemName : '',
        style: TextStyle(
          color: state.mode == HousePickerMode.UNIT
              ? Colors.blueAccent
              : Colors.black,
        ),
      ),
      onPressed: () {
        if (state.selectedUnitInfo != null) {
          dispatch(VillageHousePickerActionCreator.chooseUnitInfo());
        }
      },
    ),
  ));
  list.add(Text('>'));

  list.add(Expanded(
    child: FlatButton(
      child: Text(
        state.selectedHouseInfo != null ? state.selectedHouseInfo.roomName : '',
        style: TextStyle(
          color: state.mode == HousePickerMode.HOUSE
              ? Colors.blueAccent
              : Colors.black,
        ),
      ),
      onPressed: () {
        if (state.selectedHouseInfo != null) {
          dispatch(VillageHousePickerActionCreator.chooseHouseInfo());
        }
      },
    ),
  ));
  list.add(Text('>'));

  return list;
}
